package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"syscall"
)

func main() {
	//openAndCloseFile()
	//readFileWithBuffer()
	//readFileWitIOUtil()
	//writeFileWithBuffer01()
	//writeFileWithBuffer02()
	//writeFileWithBuffer03()
	readAndWriteFileWithBuffer04()

}

// openAndCloseFile
//
//	@Description: 打开并关闭文件
func openAndCloseFile() {
	fmt.Println("-------------打开并关闭文件-------------")
	var path string = "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\001.png"
	//打开文件
	file, err := os.Open(path)
	if err != nil {
		fmt.Println("open file err =", err)
		return
	}
	fmt.Println("file=", file)

	err = file.Close()
	if err != nil {
		fmt.Println("close file err =", err)
	}
}

// readFileWithBuffer
//
//	@Description: 读取文件，使用缓冲reader
func readFileWithBuffer() {
	fmt.Println("-------------读取文件，使用缓冲reader-------------")
	var path string = "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\test01.txt"
	file, err := os.Open(path)
	if err != nil {
		fmt.Println("open file err =", err)
		return
	}
	defer file.Close()

	reader := bufio.NewReader(file)
	for {
		line, err := reader.ReadString('\n')
		if err == io.EOF {
			break
		}
		fmt.Print(line)

	}

}

// readFileWitIOUtil
//
//	@Description: 一次性读取文件，适合文件不大的场景
func readFileWitIOUtil() {
	fmt.Println("-------------一次性读取文件，适合文件不大的场景[ioutil]-------------")
	var path string = "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\test01.txt"
	bytes, err := ioutil.ReadFile(path)
	if err != nil {
		fmt.Println("read file err =", err)
		return
	}
	fmt.Print(string(bytes))
}

// writeFileWithBuffer01
//
//	@Description: 只写|创建文件
func writeFileWithBuffer01() {
	var filePath string = "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\test03.txt"

	//flag~
	//O_RDONLY int = syscall.O_RDONLY // 只读模式打开文件
	//O_WRONLY int = syscall.O_WRONLY // 只写模式打开文件
	//O_RDWR   int = syscall.O_RDWR   // 读写模式打开文件
	//O_APPEND int = syscall.O_APPEND // 写操作时将数据附加到文件尾部
	//O_CREATE int = syscall.O_CREAT  // 如果不存在将创建一个新文件
	//O_EXCL   int = syscall.O_EXCL   // 和O_CREATE配合使用，文件必须不存在
	//O_SYNC   int = syscall.O_SYNC   // 打开文件用于同步I/O
	//O_TRUNC  int = syscall.O_TRUNC  // 如果可能，打开时清空文件

	//FileMode:https://studygolang.com/static/pkgdoc/pkg/os.htm#FileMode windows无效
	//只写|创建文件

	file, err := os.OpenFile(filePath, syscall.O_WRONLY|syscall.O_CREAT, 0666)
	if err != nil {
		fmt.Println("openFile err =", err)
		return
	}
	defer file.Close()
	writer := bufio.NewWriter(file)
	str := "hello,golang\r\n"
	for i := 0; i < 5; i++ {
		writer.WriteString(str)
	}
	err = writer.Flush()
	if err != nil {
		return
	}

}

// writeFileWithBuffer02
//
//	@Description: 只写|清空
func writeFileWithBuffer02() {
	var filePath string = "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\test03.txt"

	//FileMode:https://studygolang.com/static/pkgdoc/pkg/os.htm#FileMode windows无效
	//只写|清空
	file, err := os.OpenFile(filePath, syscall.O_WRONLY|syscall.O_TRUNC, 0666)
	if err != nil {
		fmt.Println("openFile err =", err)
		return
	}
	defer closeFile(file)

	writer := bufio.NewWriter(file)
	str := "hello,中国\r\n"
	for i := 0; i < 5; i++ {
		_, err := writer.WriteString(str)
		if err != nil {
			return
		}
	}
	err = writer.Flush()
	if err != nil {
		return
	}

}

// writeFileWithBuffer03
//
//	@Description: 只写|追加
func writeFileWithBuffer03() {
	var filePath string = "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\test03.txt"

	//FileMode:https://studygolang.com/static/pkgdoc/pkg/os.htm#FileMode windows无效
	//只写|追加
	file, err := os.OpenFile(filePath, syscall.O_WRONLY|syscall.O_APPEND, 0666)
	if err != nil {
		fmt.Println("openFile err =", err)
		return
	}
	defer closeFile(file)

	writer := bufio.NewWriter(file)
	str := "hello,golang-append\r\n"
	for i := 0; i < 5; i++ {
		writer.WriteString(str)
	}
	writer.Flush()

}

// readAndWriteFileWithBuffer04
//
//	@Description: 读写|追加
func readAndWriteFileWithBuffer04() {
	var filePath string = "E:\\goprojects\\src\\helloworld01\\src\\helloword01\\24file\\00_testfile\\test03.txt"

	//FileMode:https://studygolang.com/static/pkgdoc/pkg/os.htm#FileMode windows无效
	//读写|追加

	file, err := os.OpenFile(filePath, syscall.O_RDWR|syscall.O_APPEND, 0666)
	if err != nil {
		fmt.Println("openFile err =", err)
		return
	}
	defer file.Close()

	reader := bufio.NewReader(file)
	for {
		line, err := reader.ReadString('\n')
		if err == io.EOF { //io.EOF 代表文件的末尾
			break
		}
		fmt.Print(line)
	}
	writer := bufio.NewWriter(file)
	str := "hello,append\r\n"
	for i := 0; i < 5; i++ {
		writer.WriteString(str)
	}
	writer.Flush()

}

// closeFile
//
//	@Description: defer关闭文件
//	@param file
func closeFile(file *os.File) {
	err := file.Close()
	if err != nil {
		fmt.Println("close file err =", err)
	}
}
